package com.sita.bike.utils;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

import com.sita.bike.R;
import com.sita.friend.ui.activity.MainActivity;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Class com.sita.bike.utils is for
 *
 * @author xiaodong on 15/3/6.
 */
public class NotificationUtils {

    private static AtomicInteger count = new AtomicInteger(0);

    private static int getNextId(){
        if (count.get() == Integer.MAX_VALUE){
            count.set(0);
        }
        return count.incrementAndGet();
    }

    public static void notifyNewTrendPublished(Context context, String title, String resourceId) {

        String message = title;

        Intent theIntent = new Intent(context, MainActivity.class)
                .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        Bundle bundle = new Bundle();
        bundle.putString("action", "notifyNewTrend");
        bundle.putString("resourceId", resourceId);
        theIntent.putExtras(bundle);
        theIntent.putExtra("a", "a");
        PendingIntent mainIntent = PendingIntent.getActivity(context, 0, theIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification = new NotificationCompat.Builder(context)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true)
                .setLargeIcon(BitmapFactory.decodeResource(context.getResources(),
                        R.mipmap.ic_launcher))
                .setTicker(message)
                .setContentTitle(message)
                .setContentText(title)
                .setContentIntent(mainIntent)
                .build();
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(getNextId(), notification);
    }

//    public static void notifyAddContact(Context context, String sender) {
//        AddFriendEvent event = new AddFriendEvent();
//        event.setFriendPhone(sender);
//        PersistUtils.saveAddFriendEvent(event);
//
//        Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
//                R.mipmap.ic_launcher);
//
//        // Setup an explicit intent for an ResultActivity to receive.
//        Intent agreeFriend = new Intent(context, AcceptContactService.class);
//        Intent decline = new Intent(context, DeclineContactService.class);
//
//        agreeFriend.putExtra("contactType", AuthContactType.FRIEND.getValue());
//
//        int notifyId = getNextId();
//        agreeFriend.putExtra("sender", sender);
//        agreeFriend.putExtra("notificationId", notifyId);
//
//        decline.putExtra("sender", sender);
//        decline.putExtra("notificationId", notifyId);
//
//        Intent main = new Intent(context, MainActivity.class);
//        main.putExtra("action", "addFriend");
//        main.putExtra("phone", sender);
//
//        PendingIntent mainIntent = PendingIntent.getActivity(context, 0, main, PendingIntent.FLAG_UPDATE_CURRENT);
//
//        PendingIntent agreeFriendIntent = PendingIntent.getService(context, 0, agreeFriend, PendingIntent.FLAG_UPDATE_CURRENT);
//
//        PendingIntent declineIntent = PendingIntent.getService(context, 0, decline, PendingIntent.FLAG_UPDATE_CURRENT);
//
//        Notification notification = new NotificationCompat.Builder(context)
//                .setSmallIcon(R.mipmap.ic_launcher)
//                .setAutoCancel(true)
//                .setLargeIcon(icon)
//                .setContentIntent(mainIntent)
//                        //For now, only add as friend is available
//                .addAction(R.mipmap.ic_done, context.getString(R.string.accept), agreeFriendIntent)
//                .addAction(R.mipmap.ic_disagree, context.getString(R.string.decline), declineIntent)
//                .setContentTitle(context.getString(R.string.contact_add_request_title))
//                .setContentText(sender + context.getString(R.string.contact_add_request_content)).build();
//
//        NotificationManager notificationManager = (NotificationManager) context
//                .getSystemService(Context.NOTIFICATION_SERVICE);
//        notificationManager.notify(notifyId, notification);
//    }



}
